Repository navigation
fix(components): 提示条标题按宽度换行,不再在字符中间被裁掉 - #91
Open
xiaoyumuxi wants to merge 1 commit into
Open
xiaoyumuxi wants to merge 1 commit into
xiaoyumuxi wants to merge 1 commit into
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What this changes
components/toast.go:提示条的标题现在和描述一样按 body 宽度换行,不再只占一行。toastMeasure同时给标题和描述换行(新增toastLines放两份栈上的换行暂存,返回值改为只返回toastGeometry,标题行和描述行放在它的title、description里)。标题行高、字号、字重沿用原来的overlayLineHeight、FontSizeM、WeightMedium;标题行数最多overlayMaxLines(8),再长就在最后一行加省略号,和描述一致。PaintToast按换行后的标题逐行画,描述放在最后一行标题下面。图标和关闭按钮仍对齐标题的第一行(有描述时原来就是这样),操作按钮仍在整行居中。components/toast.go(顺带修的同一处):没有关闭按钮(DismissID为零)时,量宽度不给关闭按钮留位置,画的时候却仍按“扣掉关闭按钮”的位置给 body 和操作按钮定右边界,于是 body 比量出来的宽度窄 28px,标题可能被裁。现在没有关闭按钮时 body 一直伸到行尾,和量出来的宽度一致;操作按钮也跟着贴到行尾。components/sonner.go:只改了一处调用和一条注释。堆叠宽度、展开时各卡片的位置(sonnerReach)都经过ToastHeight,它走toastMeasure,所以标题的行数自动算进去,不需要另改偏移公式。components/toast_test.go:新增测试,见下。Why
toastMeasure只给描述换行,标题宽度超过toastMaxWidth(384)减去内边距、图标、关闭按钮、操作按钮之后就没有地方去,Label的范围比文字窄,平台把文字在字符中间裁断。[data-title]只有font-weight、line-height、color,没有white-space: nowrap、text-overflow,内容区[data-content]是flex: 1; min-width: 0,所以标题换行、不省略(Base UI 的Toast.Title也是普通块元素)。因此选换行,不选省略号。How it was verified
test -z "$(gofmt -l $(git ls-files '*.go'))"、go vet ./...通过。改动只在components的纯 Go 代码里,没有碰平台后端,没有跑平台 vet。components/toast_test.go新增测试:TestToastLongTitleWrapsInsideTheBodyAndTheToastGrows:长标题换成多行,每行不比 body 宽,高度 = 内边距 + 行数 × 行高,底边不动,换行后把各行拼回去等于原标题;短标题高度仍是2×内边距 + 一行。TestToastWrappedTitleKeepsTheDescriptionBelowAndIconAndCloseOnTheFirstLine:描述在最后一行标题下面,图标和关闭按钮的中心在第一行标题的中心。TestToastTitleNeverOverflowsItsLabelWhateverSitsBesideIt:有无关闭按钮、有无图标、带操作按钮、窗口很窄、顶部位置七种组合下,标题行都装得进它的 label,ToastHeight等于画出来的高度。TestToastEndlessTitleIsCutToTheLineLimitWithAnEllipsis、TestToastEmptyTitleKeepsItsLine:超长标题最多 8 行并以省略号结尾;空标题仍占一行。TestToastBodyHoldsItsTitleWithoutACloseButton:没有关闭按钮时短标题也装得进 label(顺带修的那处)。TestSonnerStackWrapsTitlesAtTheSharedWidthAndStacksTheirHeights:堆叠里一张长标题卡、两张短标题卡:宽度共享,短标题仍是一行;展开时总高度 = 各卡片高度 + 间隙,贴着视口边;折叠时按每张卡 5% 缩小、10px 上提算出的最高处等于长卡换行后的高度;顶部位置同样成立。TestToastWrappedTitlePaintIsAllocationFreeAfterWarmup:换行的标题单画和在堆叠里画,预热后 0 次分配。toast.go、sonner.go换回 67f9fe4 的版本)上面八个测试里有六个失败;空标题和零分配两个两边都通过,是防回退用的。go test ./... -count=1:8 个包共 39 个测试失败,在 67f9fe4 上原样存在,改动前后逐个相同,与本改动无关:base(指南与 Table)、cmd/viva(生成文件过期与.app打包)、components(只有TestDocCatalogCoversEveryComponent)、internal/gallery、internal/layering、internal/platform/darwin(链接缺 IOSurface)、tools/docs、tools/visualparity。改动后的第一次完整运行里,ui/video的TestDecoderPausesAndResumes失败过一次(时序相关),单独连跑 5 次、再完整跑一次都通过;基线那次没有出现,所以它不在 39 个里。go test ./ui -bench BenchmarkToasts -benchmem -count=6(ui.Toasts走PaintSonner):改动前 4.55–4.59µs/op,改动后 4.57–4.62µs/op(有一次 4.97µs 的离群值),前后都是 0 allocs/op。Visual evidence
-tags vivahead构建一个只有ui.Toaster的小程序(Show("Merged into the library: 12 lines from Codex's instruction file", "", ui.ToastSuccess)等三条,Expanded = true、Timeout = 0),VIVA_HEADLESS=1 VIVA_HEADLESS_SCALE=2,依次settle、shot 路径。PaintSonner绘制列表。toast_test.go测试。Where the boundary sits
提示条的宽度和换行属于框架的组件配方,和描述的换行是同一件事,应在 Viva 里修,不在 Lithe 应用层给标题加省略或缩短文案的绕过代码。合入后 Lithe 更新固定的 commit;同一分支会向上游 vivaui/viva 提 PR。
Cost
不使用提示条的程序没有开销。使用时:每次画提示条多一次标题换行(标题没超宽时不生成字符串、不分配内存),
toastLines在栈上,比原来多 128 字节。Known limits
viva add toast复制出去的模板cmd/viva/templates/toast.go.txt由tools/componentgen生成,主干上已经过期(基线里TestGeneratedRecipesAreCurrent就失败,列出的过期文件包括它),这次没有重新生成,免得把无关的模板一起带进来。